package com.dkm.orproject.dkmoa.action;

import com.alibaba.fastjson.JSONObject;
import com.dkm.orproject.dkmoa.domain.*;
import com.dkm.orproject.dkmoa.service.OaNewsService;
import com.dkm.orproject.dkmoa.service.OaNewsTypeService;
import com.dkm.orproject.utils.jwt.login.UserLoginToken;
import com.dkm.orproject.utils.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @PROJECT_NAME: orproject
 * @DESCRIPTION: 新闻类型的模块
 * @USER: zy
 * @DATE: 2020/8/4 9:51
 */
@RestController
@RequestMapping("/NewsType")
public class OaNewsTypeAction {
    @Autowired
    OaNewsTypeService oaNewsTypeService;
    @Autowired
    OaNewsService oaNewsService;
    @Resource
    Tools tools;

    /**
     * 分页查询
     * @param newsType 新闻类型模型用于条件查询
     * @param page 当前页数
     * @param limit 显示行数
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 查询结果
     */
    @UserLoginToken
    @GetMapping("selectAll")
    public Map<String,Object> findAll(OaNewsType newsType, Integer page, Integer limit, String startTime, String endTime,HttpServletRequest request){
        try {
            //分页查询集合
            List<OaNewsType> logs=oaNewsTypeService.selectAll(newsType,startTime,endTime,page,limit);
            //查询总行数
            Long rowCount=oaNewsTypeService.rowCount(newsType,startTime,endTime);
            //返回集合
            return Tools.maps(logs,rowCount);
        } catch (Exception e) {
            OaPersonnel oaPersonnel= (OaPersonnel)request.getSession().getAttribute("user");
            Map<String,Object> map=new HashMap<>();
            OaLog log = new OaLog();
            log.setLogTitle("查询新闻类型的所有方法");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setCreatedBy(oaPersonnel.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 新增新闻类型
     * @param oaNewsType 新闻类型模型
     * @param request 服务器请求
     * @return 回调函数
     */
    @UserLoginToken
    @PostMapping("addOrUpdateNewsType")
    public Map<String,Object> addOrUpdateNewsType(OaNewsType oaNewsType, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        OaPersonnel user = (OaPersonnel) request.getSession().getAttribute("user");
        if(ObjectUtils.isEmpty(user)){
            tools.rowMap(map,0,null);
        }
        try {
            int rows=0;
            OaLog log = new OaLog();
            //如果id不为空 那么就修改 否则增加
            if(oaNewsType.getPkNewsTypeId()!=null &&!"".equals(oaNewsType.getPkNewsTypeId())){
                //传入更新人
                oaNewsType.setUpdatedBy(user.getPersonnelName());
                //传入更新时间
                oaNewsType.setUpdatedTime(new Date());
                log.setLogTitle("新闻类型修改模块");
                rows = oaNewsTypeService.updateByPrimaryKeySelective(oaNewsType);
            }else{
                oaNewsType.setCreatedBy(user.getPersonnelName());
                oaNewsType.setIsRevision("1");
                log.setLogTitle("新闻类型增加模块");
                rows = oaNewsTypeService.insertSelective(oaNewsType);
            }
            log.setCreatedBy(user.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("新闻类型增加或修改模块异常");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            tools.errorMap(map, log);
            return map;
        }
    }

    /**
     * 根据id查询出新闻类型对象
     * @param pkNewsTypeId 新闻类型id
     * @param session
     * @param request
     * @return 返回一个新闻类型对象
     */
    @GetMapping("findById/{pkNewsTypeId}")
    @UserLoginToken
    public Map<String, Object> selectById(@PathVariable("pkNewsTypeId") Long pkNewsTypeId, HttpSession session, HttpServletRequest request) {
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        Map<String, Object> map = new HashMap<>();
        try {
            OaNewsType oaNewsType = oaNewsTypeService.selectByPrimaryKey(pkNewsTypeId);
            OaLog log = new OaLog();
            log.setLogTitle("新闻类型根据id查询模块查询成功");
            log.setLogIp(request.getRemoteAddr());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setLogType("1");
            tools.rowMap(map, 1, log);
            map.put("data",oaNewsType);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("新闻类型根据id查询模块异常");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogType("0");
            log.setLogIp(request.getRemoteAddr());
            tools.errorMap(map,log);
            return map;
        }
    }

    /**
     * 删除控制器
     * @param pkNewsTypeId 新闻类型主键
     * @param session 登录人信息
     * @param request 请求
     * @return 返回结果
     */
    @UserLoginToken
    @DeleteMapping("deleteNewsType/{pkNewsTypeId}")
    public Map<String, Object> deleteNewsType(@PathVariable("pkNewsTypeId") Long pkNewsTypeId, HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            OaLog log = new OaLog();
            OaNewsType oaNewsType=new OaNewsType();
            oaNewsType.setPkNewsTypeId(pkNewsTypeId);
            oaNewsType.setIsRevision("0");
            int  rows= oaNewsTypeService.updateByPrimaryKeySelective(oaNewsType);
            //删除成功就删除新闻类型下的新闻
            if(rows>0){
                OaNews oaNews=new OaNews();
                oaNews.setIsRevision("0");
                oaNews.setNewsTypeId(pkNewsTypeId);
                oaNewsService.updateIsva(oaNews);
            }
            log.setLogTitle("新闻类型的删除模块");
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("1");
            tools.rowMap(map, rows, log);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("新闻类型的删除模块异常");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            tools.errorMap(map,log);
            return map;
        }
    }
    /**
     * 批量删除
     * @param ids 新闻类型id的集合
     * @param session 操作人的session
     * @param request 请求
     * @return 返回结果
     */
    @DeleteMapping("deleteAll")
    @UserLoginToken
    Map<String, Object> deleteAll(@RequestParam(value = "ids[]", required = false) Long[] ids, HttpSession session, HttpServletRequest request) {
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        Map<String,Object> map=new HashMap<>();
        List<Long> list;
        if (ObjectUtils.isEmpty(ids)){
            return tools.rowMap(map,0,null);
        }else {
            list= Arrays.asList(ids);
        }
        try {
            int deleteAll = oaNewsTypeService.deleteAll(list);
            //删除成功就删除新闻类型下的新闻
            if(deleteAll>0){
                oaNewsService.deleteTypeAll(list);
            }
            OaLog log = new OaLog();
            log.setLogTitle("新闻类型批量删除模块");
            log.setLogIp(request.getRemoteAddr());
            log.setLogBody(JSONObject.toJSON(user).toString());
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("1");
            tools.rowMap(map, deleteAll, log);
            map.put("data",deleteAll);
            return map;
        } catch (Exception e) {
            OaLog log = new OaLog();
            log.setLogTitle("新闻类型批量删除模块异常");
            log.setLogIp(request.getRemoteAddr());
            log.setLogType("0");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setCreatedBy(user.getPersonnelName());
            tools.errorMap(map,log);
            return map;
        }
    }
    @GetMapping("selectValid")
    public Map<String,Object> selectValid(HttpSession session, HttpServletRequest request){
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            List<OaNewsType> all = oaNewsTypeService.selectValid();
            return Tools.maps(all, null);
        } catch (Exception e) {
            Map<String, Object> map = new HashMap<>();
            OaLog log = new OaLog();
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogIp(request.getRemoteAddr());
            log.setLogTitle("新闻类型查询模块");
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            return tools.errorMap(map,log);
        }
    }
    @GetMapping("selectTypeAndNews")
    public Map<String,Object> selectTypeAndNews(HttpSession session, HttpServletRequest request){
        OaPersonnel user = (OaPersonnel) session.getAttribute("user");
        try {
            Map<String,Object> map=new ConcurrentHashMap<>();
            List<Map<String,Object>> list = oaNewsTypeService.selectTypeAndNews();
            map.put("data",list);
            return map;
        } catch (Exception e) {
            Map<String, Object> map = new HashMap<>();
            OaLog log = new OaLog();
            log.setCreatedBy(user.getPersonnelName());
            log.setLogType("0");
            log.setLogBody(JSONObject.toJSON(e.getStackTrace()).toString());
            log.setLogIp(request.getRemoteAddr());
            log.setLogTitle("新闻类型根据类型id查询新闻的模块报错信息");
            return tools.errorMap(map,log);
        }
    }
}
